# coding: utf-8
# @Author: ChuiZi
import requests
from lib.common.baseFunc import callSelf


class RequestsCommon:
    """
    request库的基础封装,需要进一步封装则可以调用该封装
    """

    def __init__(self, meth: str, url: str, bodyType: str = 'json', body: dir = {}, headers: dir = {}, params=None,
                 cookies: str = None, timeout: int = 3, session=requests.session()):
        """
        :param meth: 入参方法
        :param url: 请求地址
        :param dataType: 请求数据类型,默认为json
        :param body: 请求体
        :param headers: 请求头
        :param cookies: 鉴权码
        :param timeout: 请求超时时间,默认为3s
        :param session: 是否处于同一会话
        """
        self.meth = meth.upper()
        self.url = url
        self.bodyType = bodyType.upper()
        self.body = body
        self.headers = headers
        self.cookies = cookies
        self.timeout = timeout
        self.session = session
        self.params = params
        self.methTypeMap = {
            # 请求方法的映射
            "GET": self.__reqMethGet,
            "POST": self.__reqMethPost,
            "DELETE": self.__reqMethDel,
        }
        self.bodyTypeMap = {
            # 数据类型的映射
            "DATA": self.__reqDataData,
            "JSON": self.__reqDataJson,
        }

    def connect(self):
        if self.meth == "":
            raise TypeError("请求方法为空,请填入正确的请求方法")
        return self.methTypeMap[self.meth]()

    def __reqMethGet(self):
        """将请求体的数据修改到url中,然后调用请求"""
        # if self.body:
        #     keyword = ''
        #     if self.body:
        #         for data_index in self.body:
        #             keyword = keyword + '{0}={1}&'.format(data_index, self.body[f'{data_index}'])
        #         self.url = self.url + '?' + keyword[:-1]
        self.params = self.body
        return self.bodyTypeMap[self.bodyType]()

    def __reqMethPost(self):
        """将请求体的数据修改到url中,然后调用请求"""
        return self.bodyTypeMap[self.bodyType]()

    def __reqMethDel(self):
        """将请求体的数据修改到url中,然后调用请求"""
        if self.body:
            for data_index in self.body:
                self.url = self.url + '/' + self.body[data_index]
        return self.bodyTypeMap[self.bodyType]()

    def __reqDataData(self):
        return self.session.request(self.meth, self.url, headers=self.headers, data=self.body, params=self.params,
                                    cookies=self.cookies, timeout=self.timeout)

    def __reqDataJson(self):
        return self.session.request(self.meth, self.url, headers=self.headers, json=self.body, params=self.params,
                                    cookies=self.cookies, timeout=self.timeout)


if __name__ == '__main__':
    heard = {
        "Host": "www.weather.com.cn",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:103.0) Gecko/20100101 Firefox/103.0",
        "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
        "Accept-Language": "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2",
        "Accept-Encoding": "gzip, deflate",
        "Connection": "keep-alive",
        "Upgrade-Insecure-Requests": "1", }
    reqBaidu = RequestsCommon('get', 'http://www.weather.com.cn/data/city3jdata/china.html', headers=heard)
    # 会出现乱码,因为使用iso8859格式读取,故需要解码,然后重新编码为utf-8,解决方案:https://www.cnblogs.com/yorshika/p/14035438.html
    print(reqBaidu.connect().content.decode("utf-8"))
    print(reqBaidu.connect().text.encode("ISO8859-1").decode("utf-8"))
